10  R: Functions

10.1 Functions in R

Functions in R are used to encapsulate reusable code. R provides a wide range of built-in functions, and users can also define their own functions.


10.1.1 Built-in Functions in R

R has many predefined functions for mathematical operations, statistics, and data manipulation.

10.1.1.1 🔹 Common Built-in Functions

sqrt(25)
[1] 5
abs(-10)
[1] 10
round(3.14159, 2)
[1] 3.14
# Character functions
toupper("hello")  # Convert to uppercase → "HELLO"
[1] "HELLO"
tolower("WORLD")  # Convert to lowercase → "world"
[1] "world"
nchar("Hello")    # Count characters → 5
[1] 5
# Logical functions
any(c(TRUE, FALSE, FALSE))   # TRUE if at least one TRUE
[1] TRUE
all(c(TRUE, TRUE, FALSE))    # FALSE if any FALSE
[1] FALSE

10.1.2 Random number generation

R provides various functions to generate random numbers from different distributions. These functions are essential for simulation, statistical modeling, and machine learning.

Function Description Example
runif(n, min, max) Uniform distribution runif(5, 0, 10)
rnorm(n, mean, sd) Normal distribution rnorm(5, mean=0, sd=1)
rbinom(n, size, prob) Binomial distribution rbinom(5, size=10, prob=0.5)
rpois(n, lambda) Poisson distribution rpois(5, lambda=3)
sample(x, size, replace) Random sampling sample(1:10, 5, replace=TRUE)

10.1.2.1 Setting a seed for reproducibility

Setting a seed ensures that random number generation produces the same results every time.

set.seed(42)
runif(3)
[1] 0.9148060 0.9370754 0.2861395

10.1.2.2 Generating uniform random numbers (runif())

The runif() function generates random numbers from a uniform distribution between a given min and max.

set.seed(42)  # Set seed for reproducibility
runif(5, min=0, max=10)
[1] 9.148060 9.370754 2.861395 8.304476 6.417455

10.1.2.3 Generating Normally Distributed Numbers (rnorm())

The rnorm() function generates random numbers from a normal (Gaussian) distribution.

set.seed(42)
rnorm(5, mean=0, sd=1)
[1]  1.3709584 -0.5646982  0.3631284  0.6328626  0.4042683

10.1.2.4 Random Sampling (sample())

The sample() function randomly selects elements from a given vector.

set.seed(42)
sample(1:10, 5, replace=TRUE)
[1]  1  5  1  9 10

10.2 Defining a function

Functions in R are defined using the keyword function(). All the statements within a function are enclosed with {} braces. Look at the function defined below. It takes an integer as an argument, and prints whether the integer is odd or even.

odd_even <- function(intgr) {
  if (intgr %% 2 == 0) {
    print("even")
  } else {
      print("odd")
  }
}

odd_even(3)
[1] "odd"

10.2.1 Function arguments

In both R and Python, functions support multiple types of arguments, including positional arguments, default arguments, variable-length arguments, and keyword arguments. The behavior of function arguments in R is nearly identical to Python.

10.2.2 Practice exercise

Write a function that returns all prime numbers between \(a\) and \(b\), where \(a\) and \(b\) are parameters of the function.

prime <- function(a, b) {
  prime_numbers <- c()
  for (number in a:b) {
    prime = 1
    
    for (factor in 2:(number - 1)) {
      if (number %% factor == 0) {
        prime = 0
      }
    }
    
    if (prime == 1) prime_numbers <- c(prime_numbers, number)
  }
  return(prime_numbers)
}
prime(40, 60)
[1] 41 43 47 53 59